﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IndoReputation.Models.BusinessObject;
using System.Data.SqlClient;
using System.Data;
using IndoReputation.Models.BusinessObject.Collections;

namespace IndoReputation.Models.DataAccess
{
    public class NewsValuesDAL
    {
        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 11/30/2011  9:39 AM
        /// Todo: Insert a Object User to Database
        /// </summary>
        /// <param name="newsValues">A user.</param>
        /// <returns></returns>
        public static int Insert(NewsValues newsValues)
        {
            try
            {
                string spName = "NewsValues_Insert";
                SqlParameter[] spPara = new SqlParameter[9];
                spPara[0] = new SqlParameter("@ID", SqlDbType.Int, 2, ParameterDirection.Output, false, 0, 0, null, DataRowVersion.Default, null);
                spPara[1] = new SqlParameter("@NewsID", newsValues.NewsID);
                spPara[2] = new SqlParameter("@NewsParameterID", newsValues.NewsParameterID);
                spPara[3] = new SqlParameter("@NewsMark", newsValues.NewsMark);
                spPara[4] = new SqlParameter("@NewsParameterValue", newsValues.NewsParameterValue);
                spPara[5] = new SqlParameter("@CreateAt", newsValues.CreateAt);
                spPara[6] = new SqlParameter("@CreateBy", newsValues.CreateBy);
                spPara[7] = new SqlParameter("@UpdateAt", DBNull.Value);
                spPara[8] = new SqlParameter("@UpdateBy", DBNull.Value);
                DataAccessBase.ExecuteNonQuery(CommandType.StoredProcedure, spName, spPara);
                return Convert.ToInt32(spPara[0].Value);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 11/30/2011  9:45 AM
        /// Todo: Update a User Exists in Database 
        /// </summary>
        /// <param name="newsValues">A user.</param>
        /// <returns></returns>
        public static bool Update(NewsValues newsValues)
        {
            try
            {
                string spName = "NewsValues_UpdateByPK";
                SqlParameter[] spPara = new SqlParameter[9];
                spPara[0] = new SqlParameter("@ID", newsValues.ID);
                spPara[1] = new SqlParameter("@NewsID", newsValues.NewsID);
                spPara[2] = new SqlParameter("@NewsParameterID", newsValues.NewsParameterID);
                spPara[3] = new SqlParameter("@NewsMark", newsValues.NewsMark);
                spPara[4] = new SqlParameter("@NewsParameterValue", newsValues.NewsParameterValue);
                spPara[5] = new SqlParameter("@CreateAt", newsValues.CreateAt);
                spPara[6] = new SqlParameter("@CreateBy", newsValues.CreateBy);
                spPara[7] = new SqlParameter("@UpdateAt", newsValues.UpdateAt);
                spPara[8] = new SqlParameter("@UpdateBy", newsValues.UpdateBy);
                int totalRecordEffected = 0;
                DataSet ds = DataAccessBase.ExecuteDataset(CommandType.StoredProcedure, spName, spPara);
                if (ds != null)
                {
                    if (ds.Tables.Count > 0)
                    {
                        if (ds.Tables[0] != null)
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                totalRecordEffected = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
                            }
                        }
                    }
                }
                if (totalRecordEffected > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception Ex)
            {
                return false;
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 11/30/2011  10:36 AM
        /// Todo: Delete a User Existst in Database
        /// </summary>
        /// <param name="user">A user.</param>
        /// <returns></returns>
        public static bool Delete(int newsValuesID)
        {
            try
            {
                string spName = "NewsValues_DeleteByPK";
                SqlParameter[] spPara = new SqlParameter[1];
                spPara[0] = new SqlParameter("@ID", newsValuesID);
                int totalRecordEffected = 0;
                DataSet ds = DataAccessBase.ExecuteDataset(CommandType.StoredProcedure, spName, spPara);
                if (ds != null)
                {
                    if (ds.Tables.Count > 0)
                    {
                        if (ds.Tables[0] != null)
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                totalRecordEffected = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
                            }
                        }
                    }
                }
                if (totalRecordEffected > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception Ex)
            {
                return false;
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 11/29/2011  5:23 PM
        /// Todo: Get All Users
        /// </summary>
        /// <returns></returns>
        public static NewsValuesList GetAllNewsValues()
        {
            try
            {
                NewsValuesList listNewsValues = null;
                string spName = "NewsValues_SelectAll";
                DataSet ds = DataAccessBase.ExecuteDataset(CommandType.StoredProcedure, spName, null);
                if (ds != null)
                {
                    if (ds.Tables.Count > 0)
                    {
                        if (ds.Tables[0] != null)
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                listNewsValues = new NewsValuesList();
                                List<NewsValues> listTemp = DataAccessBase.ConvertToList<NewsValues>(ds.Tables[0]);
                                foreach (NewsValues _r in listTemp)
                                    listNewsValues.Add(_r);
                            }
                        }
                    }
                }
                return listNewsValues;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 11/30/2011  10:40 AM
        /// Todo: Select a User existst in Database
        /// </summary>
        /// <param name="newsValuesID">The user ID.</param>
        /// <returns></returns>
        public static NewsValues GetNewsValuesById(int newsValuesID)
        {
            try
            {
                NewsValues newsValues = null;
                string spName = "NewsValues_SelectByPK";
                SqlParameter[] spPara = new SqlParameter[1];
                spPara[0] = new SqlParameter("@ID", newsValuesID);
                DataSet ds = DataAccessBase.ExecuteDataset(CommandType.StoredProcedure, spName, spPara);
                if (ds != null)
                {
                    if (ds.Tables.Count > 0)
                    {
                        if (ds.Tables[0] != null)
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                newsValues = DataAccessBase.ConvertToEntity<NewsValues>(ds.Tables[0].Rows[0]);
                            }
                        }
                    }
                }
                return newsValues;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        public static List<NewsValuesJoin> GetAllNewsValuesJoinByNewsID(int newsID)
        {
            try
            {
                List<NewsValuesJoin> listNewsValues = null;
                string spName = "NewsValues_SelectJoinByNewsID";
                SqlParameter[] spPara = new SqlParameter[1];
                spPara[0] = new SqlParameter("@ID", newsID);
                DataSet ds = DataAccessBase.ExecuteDataset(CommandType.StoredProcedure, spName, spPara);
                if (ds != null)
                {
                    if (ds.Tables.Count > 0)
                    {
                        if (ds.Tables[0] != null)
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                listNewsValues = DataAccessBase.ConvertToList<NewsValuesJoin>(ds.Tables[0]);
                            }
                        }
                    }
                }
                return listNewsValues;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
    }
}